package simpledb.planner;

import simpledb.parse.BadSyntaxException;
import simpledb.query.Plan;

class TempTableMgr {
   private String ttm_string [];
   private Plan ttm_plan[];
   private int iter;
   
   public TempTableMgr () {
      this.ttm_string = new String[100];
      this.ttm_plan = new Plan[100];
      this.iter = 0;
   }
   
   public String Insert ( Plan tablePlan )
   {
	   String tableName = "tmp" + iter;
	   ttm_string [iter] = tableName;
	   ttm_plan [iter] = tablePlan;
	   iter++;
	   if (iter > 100) iter = 0;
	   return tableName;
   }
   
   public Plan GetPlan ( String tableName )
   {
	   Plan p = ttm_plan[0];
	   Boolean found = false;
	   for (int i=0; i<100 && !found; i++)
	   {
		   if ( tableName.equals(ttm_string[i]) )
		   {
			   p = ttm_plan[i];
			   found = true;
		   }
	   }
	   if (!found) throw new BadSyntaxException();
	   return p;
   }
}
